var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var db = require('./db/db')
var expressJwt = require('express-jwt')
var { PUBLICKEY } = require('./utils/store')
var history = require('connect-history-api-fallback');
var proxy = require('express-http-proxy');
let log = require('cf-nodejs-logging-support');
log.setLoggingLevel("info");
var compression = require('compression')

var indexRouter = require('./routes/index');
var policyConditionRouter = require('./routes/policyCondition');
var btpRouter = require('./routes/btp');
var usersRouter = require('./routes/users');
var requestRouter = require('./routes/request');
var policysRouter = require('./routes/policys');
var tagsRouter = require('./routes/tags');
var portraitsRouter = require('./routes/portraits');
var talentsRouter = require('./routes/talents');
var recommendListsRouter = require('./routes/recommendLists')
var questionnaireRouter = require('./routes/questionnaires')
var questionRouter = require('./routes/questions')
var commessageRouter = require('./routes/comessages')
var historicalRouter = require('./routes/historical')
var uploadRouter = require("./routes/upload")
var finRouter = require('./routes/fin')
var assetRouter = require('./routes/assets')
var techRouter = require('./routes/techs')
var favoriteRouter = require("./routes/favorite")
var combinedRouter = require("./routes/combined")
var caijingyunRouter = require("./routes/caijingyun")
var cooperateRouter = require("./routes/cooperate")
var worktableRouter = require("./routes/worktable")
var companyVariantRouter = require("./routes/companyVariant")
var privateCommentsRouter = require("./routes/privateComments")
var userConfigRouter = require("./routes/userConfig")
var wxRouter = require('./routes/wx')
var fileListRouter = require('./routes/filelists')
var universeRouter = require('./routes/universe')
var verifyCodeRouter = require('./routes/verifyCode')
var subscribeRouter = require('./routes/subscribe')
var feedbackRouter = require('./routes/feedback')
var policyHelpRouter=require('./routes/policyHelp')
var wechatRouter = require('./routes/wechat')
var companyPortaitRouter=require('./routes/companyPortrait')
var reporting=require('./routes/reporting')

var swaggerInstall = require('./utils/swagger')
var app = express();
swaggerInstall(app)

app.use(compression());

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.set('port', process.env.PORT || 4000);
app.use(history());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//configure JWT service

app.use(function (req, res, next) {
  res.set("Access-Control-Allow-Origin", "*");
  res.set("Access-Control-Expose-Headers", "Authorization,");
  res.set("Access-Control-Allow-Headers", "Authorization, Content-Type");
  next()
})

app.use(expressJwt({
  credentialsRequired: false,
  secret: PUBLICKEY,
  algorithms: ['HS256', 'RS256']
}).unless({
  path: ['/login', '/api/fins/', '/api/policys/', '/api/techs/', '/api/users/login', '/api/users/register', '/api/tags', '/api/talents/get/', '/api/historical', '/api/wechat/scan-login'] //添加不需要验证的路由
}));


app.use(log.logNetwork);
app.use('/', indexRouter);
app.use('/api/upload', uploadRouter);
app.use('/api/feedback', feedbackRouter);
app.use('/api/policyCondition', policyConditionRouter);
app.use('/api/subscribe', subscribeRouter);
app.use('/api/btp', btpRouter);
app.use('/api/request', requestRouter);
app.use('/api/caijingyun', caijingyunRouter);
app.use('/api/users', usersRouter);
app.use('/api/policys', policysRouter);
app.use('/api/tags', tagsRouter);
app.use('/api/talents', talentsRouter);
app.use('/api/portraits', portraitsRouter);
app.use('/api/recommendLists', recommendListsRouter);
app.use('/api/historical', historicalRouter);
app.use('/api/fins', finRouter);
app.use('/api/assets', assetRouter);
app.use('/api/techs', techRouter);
app.use('/api/questionnaires', questionnaireRouter);
app.use('/api/questions', questionRouter);
app.use('/api/favorite', favoriteRouter);
app.use('/api/combined', combinedRouter);
app.use('/api/cooperate', cooperateRouter);
app.use('/api/worktable', worktableRouter);
app.use('/api/comessages', commessageRouter);
app.use('/api/companyVariant', companyVariantRouter);
app.use('/api/privateComments', privateCommentsRouter);
app.use('/api/userConfig', userConfigRouter);
app.use('/api/wx', wxRouter);
app.use('/api/universe', universeRouter);
app.use('/api/fileList', fileListRouter);
app.use('/api/wechat', wechatRouter);
app.use('/api/policysHelp', policyHelpRouter);
app.use('/api/phoneValidation', verifyCodeRouter);
app.use('/api/companyPortait',companyPortaitRouter)
app.use('/recommendApi', proxy("https://recommend.qyfn.jiangdu.cn/"));
app.use('/sms', proxy("http://114.218.57.125:7862/"));
app.use('/userAuth', proxy("http://58.220.131.72:7777/"));
app.use('/userCheck', proxy("http://58.220.131.72:12230/"));
app.use('/universeFiles', proxy("https://jiangdutest-1307977142.cos.ap-shanghai.myqcloud.com/", {
  proxyReqPathResolver: function (req) {
    return req.url
  }
}))
app.use('/api/reporting', reporting);
///universeFiles/asset/JD-BACKEND-SAP%20Cloud%20Platform%E7%B3%BB%E5%88%97%E6%96%87%E6%A1%A3-%20SAP%E4%BA%91%E5%B9%B3%E5%8F%B0%E5%88%9D%E5%A7%8B%E8%AE%BE%E7%BD%AE%E6%89%8B%E5%86%8C.pdf

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
